/*
 * Created on 2005-4-27
 *
 */
package com.hd123.rumba.runtime;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.net.Socket;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;

import com.hd123.rumba.request.ActionConstants;
import com.hd123.rumba.request.JspLib;

/**
 * @author caili
 */
public class Runtime {
  // static {
  // java.lang.Runtime.getRuntime().loadLibrary("rumba.dll");
  // }

  // public static native String getLicense(String name);
  public static String getLicense(String name) {
    String result = "";
    Socket sck = null;
    DataInputStream in = null;
    DataOutputStream out = null;
    try {
      sck = new Socket("127.0.0.1", 8088);
      in = new DataInputStream(sck.getInputStream());
      out = new DataOutputStream(sck.getOutputStream());
      String answer = Double.toString(Math.random());
      String msg = "getLicense\u0001" + name + "\u0001" + answer + "\u0001true";
      byte[] crln = new byte[] {
          0x0d, 0x0a };
      out.write(msg.getBytes());
      out.write(crln);
      out.write(crln);
      out.flush();

      byte[] recv = new byte[2000];
      int len = in.read(recv, 0, 2000);

      in.close();
      out.close();
      sck.close();

      result = new String(recv, 0, len - 4);
      if (result != null && answer.equals(result.substring(0, answer.length()))) {
        result = result.substring(answer.length());
      }
    } catch (Exception e) {
      Logger.getLogger(Runtime.class).error("许可证验证错误: " + e.getMessage());
    }
    return result;
  }

  public static String getAction(String url) {
    return ActionConstants.ACTIONURL + url;
  }

  public static String getExternalAction(String url) {
    return ActionConstants.EXTERNACTIONURL + url;
  }

  public static String getTemplate(String relativeUrl) {
    StringTokenizer st = new StringTokenizer(ActionConstants.TEMPLATEDIR, ";");
    String[] templateDirs = new String[st.countTokens()];
    for (int i = 0; st.hasMoreTokens(); ++i)
      templateDirs[i] = st.nextToken();

    String jspUri = ActionConstants.TEMPLATEURL + relativeUrl;
    if (Runtime.class.getResource(jspUri) == null) {
      Logger log = Logger.getLogger(Runtime.class);
      for (int i = -1; i < templateDirs.length; ++i) {
        if (i == -1)
          jspUri = ActionConstants.TEMPLATEURL + relativeUrl;
        else
          jspUri = ActionConstants.TEMPLATEURL + templateDirs[i] + "/" + relativeUrl;
        if (log.isDebugEnabled())
          log.debug("Search jsp uri: " + jspUri);
        if (Runtime.class.getResource(jspUri) != null
            || JspLib.class.getResource("/../.." + jspUri) != null) {
          break;
        } else {
          File file = new File(ActionConstants.APPHOME, jspUri);
          if (file.exists()) {
            break;
          }
        }
      }
    }
    return jspUri;
  }

  public static boolean checkLicense(String name) throws Exception {
    try {
      String license = getLicense(name);
      if (license != null && license.length() > 0) {
        License.setLicense(license);
        return true;
      }
    } catch (Exception e) {
      Logger.getLogger(Runtime.class).error("许可证验证错误: " + e.getMessage());
    }
    Logger.getLogger(Runtime.class).error("许可证无效");
    return false;
  }
}
